package com.hbzhit.config;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.LFUCache;
import cn.hutool.core.date.DateUtil;
import com.hbzhit.modules.sys.entity.*;
import com.hbzhit.modules.sys.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 缓存配置
 * @author DING
 */
@Slf4j
@Configuration
@AllArgsConstructor
public class CacheConfig {

    private final SysUserService sysUserService;
    private final SysOrgService sysOrgService;
    private final SysDictService sysDictService;
    private final SysDictValService sysDictValService;
    private final SysMenuService sysMenuService;
    private final SysConfigService sysConfigService;

    /**
     * 用户列表缓存，参数0, 0表示数量和有效时间无限制
     */
    public static LFUCache<Long, SysUserEntity> userCache = CacheUtil.newLFUCache(0, 0);

    /**
     * 组织机构列表缓存，参数0, 0表示数量和有效时间无限制
     */
    public static LFUCache<Long, SysOrgEntity> orgCache = CacheUtil.newLFUCache(0, 0);

    /**
     * 数据字典类别列表缓存，参数0, 0表示数量和有效时间无限制
     */
    public static LFUCache<Long, SysDictEntity> dictCache = CacheUtil.newLFUCache(0, 0);

    /**
     * 数据字典内容列表缓存，参数0, 0表示数量和有效时间无限制
     */
    public static LFUCache<Long, SysDictValEntity> dictValCache = CacheUtil.newLFUCache(0, 0);

    /**
     * 菜单列表缓存，参数0, 0表示数量和有效时间无限制
     */
    public static LFUCache<Long, SysMenuEntity> menuCache = CacheUtil.newLFUCache(0, 0);

    /**
     * 配置管理列表缓存，参数0, 0表示数量和有效时间无限制
     */
    public static LFUCache<String, SysConfigEntity> confCache = CacheUtil.newLFUCache(0, 0);


    /**
     * 缓存初始化
     */
    @Bean
    public void cacheInit() {
        this.createCache();
    }

    /**
     * 创建缓存数据
     */
    public void createCache() {
        log.info("开始创建数据缓存......");
        long start = DateUtil.current();

        //创建用户缓存
        List<SysUserEntity> users = sysUserService.listAll();
        userCache.clear();
        users.forEach(user -> userCache.put(user.getId(), user));

        //创建组织机构缓存
        List<SysOrgEntity> orgs = sysOrgService.listAll();
        orgCache.clear();
        orgs.forEach(org -> orgCache.put(org.getOrgId(), org));

        //创建数据字典类别缓存
        List<SysDictEntity> dicts = sysDictService.listAll();
        dictCache.clear();
        dicts.forEach(dict -> dictCache.put(dict.getId(), dict));

        //创建数据字典内容缓存
        List<SysDictValEntity> dictVals = sysDictValService.listAll();
        dictValCache.clear();
        dictVals.forEach(dictVal -> dictValCache.put(dictVal.getId(), dictVal));

        //创建菜单缓存
        List<SysMenuEntity> menus = sysMenuService.listAll();
        menuCache.clear();
        menus.forEach(menu -> menuCache.put(menu.getId(), menu));

        //创建配置管理缓存
        List<SysConfigEntity> confs = sysConfigService.list();
        confCache.clear();
        confs.forEach(conf -> confCache.put(conf.getParamKey(), conf));

        long end = DateUtil.current();
        long time = end - start;
        log.info("创建数据缓存完成，耗时{}毫秒", time);
    }
}
